package Main;
/**
 * @author ARMGARCES
 *
 */
import Objetos.Sesion;
import Pantallas.PantallaInicio;
import Pantallas.PantallaSesionIniciada;
import Pantallas.PantallaSesionRestaurada;
import Pantallas.VentanaInfo;
import WSS.Wss;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.UiApplication;
/**
 * This class extends the UiApplication class, providing a
 * graphical user interface.
 */
public class MyApp extends UiApplication{
	//
	private static MyApp instance;
	private static PantallaInicio ppal;
	private static Sesion sesion;
	private static int width;
	private static int height;
    /**
     * Entry point for application
     * @param args Command line arguments (not used)
     */ 
    public static void main(String[] args)
    {
        // Create a new instance of the application and make the currently
        // running thread the application's event dispatch thread.
        MyApp theApp = MyApp.getInstance();      
        //theApp.addKeyListener (new ShortcutHandler());
        theApp.displayPantallaPpal();
        theApp.enterEventDispatcher();
    }
 
    /**
     * Creates a new MyApp object
     */
    private MyApp()
    {        
        // Push a screen onto the UI stack for rendering.
    	width  = Display.getWidth();
    	height = Display.getHeight();
    	ppal = new PantallaInicio();
    }   
    /**
     * 
     * @return
     */
    public static synchronized MyApp getInstance(){
		if (instance == null)
			instance = new MyApp();
		return instance;
	}
    /**
     * 
     */
    public void displayPantallaPpal(){    	    	
    	
    	pushScreen(getPantallaPpal());
    	//
    }
    /**
     * 
     * @return
     */
    public static PantallaInicio getPantallaPpal(){
    	return ppal;
    }
    /**
     * 
     * @param _sesion
     */
    private void setSesion(Sesion _sesion){
    	MyApp.sesion = _sesion;
    }
    /**
     * 
     * @return
     */
    public Sesion getSesion(){
    	return MyApp.sesion;
    }
    /**
	 * @return the width
	 */
	public static int getWidth() {
		return width;
	}

	/**
	 * @param width the width to set
	 */
	public static void setWidth(int width) {
		MyApp.width = width;
	}

	/**
	 * @return the height
	 */
	public static int getHeight() {
		return height;
	}

	/**
	 * @param height the height to set
	 */
	public static void setHeight(int height) {
		MyApp.height = height;
	}

	/**
     * 
     * @param _codtel
     */
    public static void instalarApp(final String _usr,final String _pswd,final String _num){
    	UiApplication.getUiApplication().invokeLater( 
			new Runnable() { 
				public void run() { 
					Admin admin = new Admin();
					boolean cont;
					cont = admin.crearBd();
					if(cont)
						Wss.registrarTelf(_usr,_pswd,_num);
				} 
			});
    }
    /**
     * 
     * @param _codtel
     */
    public static void contInstalarApp(final int _codtel){
    	UiApplication.getUiApplication().invokeLater( 
			new Runnable() { 
				public void run() { 
					Admin admin = new Admin();
					boolean cont;
					if(_codtel > 0){
						cont = admin.insertAdmin(_codtel);
					}else{
						cont = false;
					}
					if(cont){
						VentanaInfo.mostrarMensaje("Diga Movil se ha instalado correctamente", "");
					}else{
						Wss.eliminarTelf(_codtel);
						VentanaInfo.mostrarMensaje("Diga Movil no pudo ser instalado", "");
					}
				} 
			});
    }
    /**
     * 
     * @param _sesion
     */
    public void iniciarSesion(Sesion _sesion){
    	this.setSesion(_sesion);
    	PantallaSesionIniciada psi1 = new PantallaSesionIniciada();
		UiApplication.getUiApplication().pushScreen(psi1);
		psi1.invalidate();
		VentanaInfo.mostrarMensaje("Sesion Iniciada el ",_sesion.getHoraInicio());
    }
    
    /**
     * 
     * @param _sesion
     */
    public void restaurarSesion(Sesion _sesion){
    	this.setSesion(_sesion);
    	PantallaSesionRestaurada psr1 = new PantallaSesionRestaurada();
		UiApplication.getUiApplication().pushScreen(psr1);
		psr1.invalidate();
		VentanaInfo.mostrarMensaje("Sesion Restaurada del ",_sesion.getHoraInicio());
    }
}
